<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: NodeRAS.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: NodeRAS.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>"use strict";

/*
 * NodeRAS is a random access storage engine that allows us to load and save
 * data at high speed whilst maintaining the ability to only read sections
 * of data from the storage. It also supports middleware to allow for
 * functionality like encryption and data compression.
 */

/*
 * Data files saved by this storage engine are composed of an initial header
 * section which contains meta-data used to identify data entry boundaries
 * and then a body section which contains the data proper.
 *
 * You can think of the data file being composed of many JSON objects. The
 * header is one object and the body is an array of multiple other objects.
 * At the binary level we always start with a data length followed by a
 * marker that denotes the length end, after which the data will follow.
 *
 * e.g. 20:{"hello": "thgbfsw"}20:{"hello": "thgbfsw"}
 *
 * The first entry will always be the header or "meta-data". The meta-data
 * includes all the information we need to locate entries in the body randomly
 * so you can think of it as the body's index. It can also contain arbitrary
 * data apart from the index that may include flags denoting middleware usage
 * etc.
 */
var NodeRAS,
	fs = require('fs');

/**
 * Create a constructor method that calls the instance's init method.
 * This allows the constructor to be overridden by other modules because
 * they can override the init method with their own.
 * @class
 * @constructor
 */
NodeRAS = function () {
	this.init.apply(this, arguments);
};

NodeRAS.prototype.init = function () {
	var self = this;

	self.opCodes = {
		"insert": "00",
		"delete": "03",
		"pointer": "99"
	};

	/*
	opcode(2)bytesize(*):data:pointer:

	0 0020:{"hello": "thgbfsw",:99223:
	...
	8834 0310:{"a":"12"}::
	...
	99223 9913:"foo": "bar"}::

	Line 8834 has opcode 03 (deleted) so that space is now empty

	Read line 0.
	Opcode: 00 (insert)
	Data byte length: 20
	Data is: {"hello": "thgbfsw",
	Pointer shows more data at position 99223

	Skip to 99223

	Read line 99223
	Opcode: 99 (continued data)
	Data byte length: 13
	Data is: "foo": "bar"}
	Pointer hold no data, end of data

	Reconstituted data is: {"hello": "thgbfsw","foo": "bar"}
	*/
};

/**
 * Appends new data to the end of the file.
 * @param filePath The file to operate on.
 * @param primaryKey The data entry's primary key.
 * @param data The data to write.
 * @param callback A callback after the operation has finished.
 */
NodeRAS.prototype.post = function (filePath, primaryKey, data, callback) {
	var self = this;

	fs.open(filePath, 'a', function(status, fd) {
		var dataLine = self.encodeDataLine('insert', data, "");

		fs.write(fd, dataLine, function(err) {
			fs.close(fd);
			callback(err);
		});
	});
};

/**
 *
 * @param filePath The file to operate on.
 * @param primaryKey The data entry's primary key.
 * @param data The data to write.
 * @param callback
 */
NodeRAS.prototype.put = function (filePath, primaryKey, data, callback) {
	var self = this;

	fs.open(filePath, 'a', function(status, fd) {
		var dataLine = self.encodeDataLine('insert', data, "");

		fs.write(fd, dataLine, function(err) {
			fs.close(fd);
			callback(err);
		});
	});
};

NodeRAS.prototype.encodeDataLine = function (opType, data, pointer) {
	pointer = pointer !== undefined ? pointer : "";
	return this.opCodes[opType] + Buffer.byteLength(data, 'utf8') + ':' + data + ":" + pointer + ":";
};

/**
 * Scans the data file specified and removes entries marked for deletion,
 * de-fragments entries that span multiple points and then re-writes the
 * file.
 *
 * @returns {Number} The number of bytes saved by the compact process.
 * This is the file size in bytes before the operation minus the file size
 * in bytes after the operation.
 */
NodeRAS.prototype.compact = function () {
	var delta = 0;

	return delta;
};

module.exports = NodeRAS;</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ActiveBucket.html">ActiveBucket</a></li><li><a href="Angular.html">Angular</a></li><li><a href="AutoBind.html">AutoBind</a></li><li><a href="Collection.html">Collection</a></li><li><a href="CollectionGroup.html">CollectionGroup</a></li><li><a href="Condition.html">Condition</a></li><li><a href="Core.html">Core</a></li><li><a href="Db.html">Db</a></li><li><a href="Document.html">Document</a></li><li><a href="Grid.html">Grid</a></li><li><a href="Highchart.html">Highchart</a></li><li><a href="Index2d.html">Index2d</a></li><li><a href="IndexBinaryTree.html">IndexBinaryTree</a></li><li><a href="IndexHashMap.html">IndexHashMap</a></li><li><a href="Infinilist.html">Infinilist</a></li><li><a href="KeyValueStore.html">KeyValueStore</a></li><li><a href="Metrics.html">Metrics</a></li><li><a href="MyModule.html">MyModule</a></li><li><a href="NodeApiClient.html">NodeApiClient</a></li><li><a href="NodeApiServer.html">NodeApiServer</a></li><li><a href="NodeRAS.html">NodeRAS</a></li><li><a href="Odm.html">Odm</a></li><li><a href="OldView.html">OldView</a></li><li><a href="Operation.html">Operation</a></li><li><a href="Overload.html">Overload</a></li><li><a href="Overview.html">Overview</a></li><li><a href="Overview_init.html">init</a></li><li><a href="Path.html">Path</a></li><li><a href="Persist.html">Persist</a></li><li><a href="Procedure.html">Procedure</a></li><li><a href="ReactorIO.html">ReactorIO</a></li><li><a href="Section.html">Section</a></li><li><a href="Serialiser.html">Serialiser</a></li><li><a href="Shared.overload.html">overload</a></li><li><a href="View.html">View</a></li></ul><h3>Mixins</h3><ul><li><a href="ChainReactor.html">ChainReactor</a></li><li><a href="Common.html">Common</a></li><li><a href="Constants.html">Constants</a></li><li><a href="Events.html">Events</a></li><li><a href="Matching.html">Matching</a></li><li><a href="Shared.html">Shared</a></li><li><a href="Sorting.html">Sorting</a></li><li><a href="Tags.html">Tags</a></li><li><a href="Triggers.html">Triggers</a></li><li><a href="Updating.html">Updating</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Mar 01 2018 11:34:22 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
